package com.eedi.framework.notify.controller.sys;

import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.notify.controller.sys.vo.template.SysNotifyTemplateSaveReq;
import com.eedi.framework.notify.service.SysNotifySendService;
import com.eedi.framework.notify.service.SysNotifyTemplateService;
import com.eedi.framework.notify.controller.sys.vo.template.SysNotifyTemplatePageReq;
import com.eedi.framework.notify.controller.sys.vo.template.SysNotifyTemplateResp;
import com.eedi.framework.notify.controller.sys.vo.template.SysNotifyTemplateSendReq;
import com.eedi.framework.notify.dal.dataobject.SysNotifyTemplateDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 站内信模版")
@RestController
@RequestMapping("/notify-template")
@Validated
public class SysNotifyTemplateController {

    @Resource
    private SysNotifyTemplateService sysNotifyTemplateService;

    @Resource
    private SysNotifySendService sysNotifySendService;

    @PostMapping("/create")
    @Operation(summary = "创建站内信模版")
    @PreAuthorize("@ss.hasPermission('sys:notify-template:create')")
    public CommonResult<String> createNotifyTemplate(@Valid @RequestBody SysNotifyTemplateSaveReq createReqVO) {
        return success(sysNotifyTemplateService.createNotifyTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站内信模版")
    @PreAuthorize("@ss.hasPermission('sys:notify-template:update')")
    public CommonResult<Boolean> updateNotifyTemplate(@Valid @RequestBody SysNotifyTemplateSaveReq updateReqVO) {
        sysNotifyTemplateService.updateNotifyTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站内信模版")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:notify-template:delete')")
    public CommonResult<Boolean> deleteNotifyTemplate(@RequestParam("id") String id) {
        sysNotifyTemplateService.deleteNotifyTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站内信模版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:notify-template:query')")
    public CommonResult<SysNotifyTemplateResp> getNotifyTemplate(@RequestParam("id") String id) {
        SysNotifyTemplateDO template = sysNotifyTemplateService.getNotifyTemplate(id);
        return success(BeanUtils.toBean(template, SysNotifyTemplateResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站内信模版分页")
    @PreAuthorize("@ss.hasPermission('sys:notify-template:query')")
    public CommonResult<PageResult<SysNotifyTemplateResp>> getNotifyTemplatePage(@Valid SysNotifyTemplatePageReq pageVO) {
        PageResult<SysNotifyTemplateDO> pageResult = sysNotifyTemplateService.getNotifyTemplatePage(pageVO);
        return success(BeanUtils.toBean(pageResult, SysNotifyTemplateResp.class));
    }

    @PostMapping("/send-notify")
    @Operation(summary = "发送站内信")
    @PreAuthorize("@ss.hasPermission('sys:notify-template:send-notify')")
    public CommonResult<String> sendNotify(@Valid @RequestBody SysNotifyTemplateSendReq sendReqVO) {
        if (UserTypeEnum.MEMBER_SYS.equals(sendReqVO.getSysUserType())) {
            return success(sysNotifySendService.sendSingleNotifyToMember(sendReqVO.getSysUserId(),
                    sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
        } else {
            return success(sysNotifySendService.sendSingleNotifyToAdmin(sendReqVO.getSysUserId(),
                    sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
        }
    }
}
